<?php if (!defined('BASEPATH'))exit('No direct script access allowed');
/**
 * 体验平台模块控制器
 */
class Mplat extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('plat_model');
        $this->load->model('users_model');
        $this->load->model('users_log_model');
        $this->assign("module", "mplat");
    }
    
    //回绑接口
    function callback(){
        $mess = array();
        $plat_id = trim($_REQUEST['platID']);//商家平台唯一标识
        $user_id = trim($_REQUEST['userID']);//弹溜溜用户唯一标识
        $puser_id = trim($_REQUEST['puserID']);//商家用户唯一标识
        $sign = trim($_REQUEST['sign']);//校验码
        $plat = $this->plat_model->get_plat(array('name_en'=>$plat_id,'is_show'=>1));
        if(!$plat){
            $mess['status'] = "002";
            $mess['errmsg'] = "platID不存在或商家已下线";
            echo JSON($mess);exit;
        }
        if(!$user_id){
            $mess['status'] = "003";
            $mess['errmsg'] = "userID不能为空";
            echo JSON($mess);exit;
        }
        $uinfo = $this->users_model->get_user_by_uid($user_id);
        if(!$uinfo){
            $mess['status'] = "003";
            $mess['errmsg'] = "userID非法参数";
            echo JSON($mess);exit;
        }
        if(!$puser_id){
            $mess['status'] = "005";
            $mess['errmsg'] = "puserID不能为空";
            echo JSON($mess);exit;
        }
        $access_key = $plat['access_key'];
        $sign_c = strtolower(md5($access_key.$plat_id.$puser_id.$user_id.$access_key));
        //校验sign
        if($sign_c != $sign){
            $mess['status'] = "006";
            $mess['errmsg'] = "sign校验失败";
            echo JSON($mess);exit;
        }
        $where = array();
        $where['user_id'] = $user_id;
        $where['plat_en'] = $plat['name_en'];
        $where['usernamep'] = $puser_id;
        //检测用户是否绑定过了
        $cpa = $this->plat_model->check_ab_cpa($where);
        
        if($cpa){
            $mess['status'] = "004";
            $mess['errmsg'] = "userID不能重复绑定";
            echo JSON($mess);exit;
        }
        $cpa_data = array();
        $cpa_data['user_id'] = $uinfo['user_id'];
        $cpa_data['plat_id'] = $plat['id'];
        $cpa_data['plat_en'] = $plat['name_en'];
        $cpa_data['usernamep'] = $puser_id;
        $cpa_data['reg_time'] = time();
        //绑定数据
        $result = $this->plat_model->insert_ab_cpa($cpa_data);
        
        //写入日志记录
        $log_data = array();
        $log_data['user_id'] = $user_id;
        $log_data['events'] = 'callback';
        $log_data['content'] = JSON($_REQUEST);
        $log_data['ip'] = $this->input->ip_address();
        $log_data['create_time'] = time();
        $this->users_log_model->insert($log_data);
        
        if($result){
            $mess['status'] = "000";
            $mess['errmsg'] = "绑定成功";
        }else{
            $mess['status'] = "001";
            $mess['errmsg'] = "绑定失败";
        }
        
        echo JSON($mess);exit;
    }
    
}
